package com.neusmart.common.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by chris on 15/6/7.
 */
public class TimeUtil {

    public static String DATE_FORMAT_YMD_HMS = "yyyy-MM-dd HH:mm:ss";
    public static String DATE_FORMAT_HMS = "HH:mm:ss";
    public static String DATE_FORMAT_MD_HM = "MM-dd HH:mm";
    public static String DATE_FORMAT_MD_E = "MM月dd日 E";
    public static String DATE_FORMAT_MD = "MM月dd日";
    public static String DATE_FORMAT_YMD_HM = "yyyy-MM-dd HH:mm";
    public static String DATE_FORMAT_YMD = "yyyy.MM.dd";
    public static String DATE_FORMAT_YMD_ = "yyyy-MM-dd";
    public static String DATE_FORMAT_Y = "yyyy";

    /**
     * Convert longTime to "yyyy-MM-dd HH:mm:ss"
     *
     * @param longTime
     * @return
     */
    public static final String convertLong2String(long longTime) {
        SimpleDateFormat format = new SimpleDateFormat(DATE_FORMAT_YMD_HMS);
        //seconds -> milliseconds
        if (String.valueOf(longTime).length() == 10)
            longTime *= 1000;
        Date date = new Date(longTime);
        String strTime = format.format(date);
        return strTime;
    }

    /**
     * Convert longTime to "yyyy.MM.dd"
     *
     * @param longTime
     * @return
     */
    public static final String convertLong2Date(long longTime) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd");
        //seconds -> milliseconds
        if (String.valueOf(longTime).length() == 10)
            longTime *= 1000;
        Date date = new Date(longTime);
        String strTime = format.format(date);
        return strTime;
    }

    /**
     * Convert longTime to required date format.
     *
     * @param longTime
     * @param dateFormat
     * @return
     */
    public static String getTimeByFormat(long longTime, String dateFormat) {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        //seconds -> milliseconds
        if (String.valueOf(longTime).length() == 10)
            longTime *= 1000;
        Date date = new Date(longTime);
        String strTime = format.format(date);
        return strTime;
    }

    public static String getTimeByFormat(Date date, String dateFormat) {
        SimpleDateFormat format = new SimpleDateFormat(dateFormat);
        return format.format(date);
    }

    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) - day);
        return now.getTime();
    }

    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

}
